﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Xml;
using System.IO;

using System.Xml.Serialization;

using XEvents.Library;
using WeifenLuo.WinFormsUI.Docking;


namespace ExtendedEventsManager
{
    public partial class MetaDataViewerForm : DockContent

    {
        private SqlServer server;

        public MetaDataViewerForm(SqlServer si)
        {
            server = si;
            InitializeComponent();
            LoadXMLTree();
            this.ToolTipText = this.TabText;
        }

        private void MetaDataViewer_Load(object sender, EventArgs e)
        {
        }

        private void LoadXMLTree()
        {
            XmlDocument xdoc = server.MetaData.GetXml();
            // SECTION 2. Initialize the TreeView control.
            treeView1.Nodes.Clear();

            foreach (XmlElement e in xdoc.DocumentElement.FirstChild.ChildNodes)
            {
                TreeNode tNode = new TreeNode(e.Attributes["Name"].Value);
                treeView1.Nodes.Add(tNode);
                //tNode.Tag = server.MetaData.GetXml().DocumentElement;

                // SECTION 3. Populate the TreeView with the DOM nodes.
                AddNode(e, tNode);
            }
        }


        private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
        {            
            XmlNode xNode;
            TreeNode tNode;
            XmlNodeList nodeList;
            int i;

            // Loop through the XML nodes until the leaf is reached.
            // Add the nodes to the TreeView during the looping process.
            if (inXmlNode.HasChildNodes)
            {
                nodeList = inXmlNode.ChildNodes;
                for (i = 0; i <= nodeList.Count - 1; i++)
                {
                    xNode = inXmlNode.ChildNodes[i];
                    if (xNode.Attributes["Name"] == null)
                        inTreeNode.Nodes.Add(new XEventTreeNode(xNode.Name, xNode));
                    else
                        inTreeNode.Nodes.Add(new XEventTreeNode(xNode.Attributes["Name"].Value, xNode));
                    tNode = inTreeNode.Nodes[i];
                    AddNode(xNode, tNode);
                }
            }
            else
            {
                // Here you need to pull the data from the XmlNode based on the
                // type of node, whether attribute values are required, and so forth.

                if (inXmlNode.Attributes["Name"] == null)
                    inTreeNode.Text = inXmlNode.Name;
                else
                    inTreeNode.Text = inXmlNode.Attributes["Name"].Value;
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode tNode = e.Node;
            XmlNode xnode = (XmlNode)tNode.Tag;
            
            object obj = new object();

            //switch (Utilities.GetParentNodeByLevel(e.Node, 1).Name)
            //{
            //    case "Actions":

            switch (e.Node.Level)
            {
                case 0:
                    obj = server.MetaData.Packages[Utilities.GetParentNodeByLevel(e.Node, 0).Text];
                    break;
                case 1:
                    obj = null;
                    break;
                case 2:
                    switch (Utilities.GetParentNodeByLevel(e.Node, 1).Text)
                    {
                        case "Actions":
                            obj = server.MetaData.Packages[Utilities.GetParentNodeByLevel(e.Node, 0).Text].Actions[e.Node.Text];
                            break;
                        case "Events":
                            obj = server.MetaData.Packages[Utilities.GetParentNodeByLevel(e.Node, 0).Text].Events[e.Node.Text];
                            break;
                        case "Maps":
                            obj = server.MetaData.Packages[Utilities.GetParentNodeByLevel(e.Node, 0).Text].Maps[e.Node.Text];
                            break;
                        case "Predicates":
                            obj = server.MetaData.Packages[Utilities.GetParentNodeByLevel(e.Node, 0).Text].Predicates[e.Node.Text];
                            break;
                        case "Targets":
                            obj = server.MetaData.Packages[Utilities.GetParentNodeByLevel(e.Node, 0).Text].Targets[e.Node.Text];
                            break;
                        case "Types":
                            obj = server.MetaData.Packages[Utilities.GetParentNodeByLevel(e.Node, 0).Text].Types[e.Node.Text];
                            break;
                        default:
                            obj = null;
                            break;
                    }
                    break;
            }

            //    server.MetaData

            string nodeInfo = string.Empty;
            if (xnode != null)
            {
                foreach (XmlAttribute xa in xnode.Attributes)
                {
                    nodeInfo += xa.Name + ": " + xa.Value + "\r\n";
                }
            }

            textBox1.Text = nodeInfo;
            ((MainForm)this.DockPanel.Parent).PropertiesForm.PropertyGrid.SelectedObject = obj;
            
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                treeView1.SelectedNode = e.Node;

                //contextMenuStrip1.Show(treeView1, e.Location);
            }
        }
    }
}
